package cn.doctorpda.conference.model;

import android.content.Context;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.UUID;

import cn.doctorpda.conference.MyApplication;
import cn.doctorpda.conference.config.AppConfig;
import cn.doctorpda.conference.okhttp.OkHttpUtils;
import cn.doctorpda.conference.okhttp.callback.Callback;
import cn.doctorpda.conference.utils.SPUtils;
import cn.doctorpda.conference.utils.ScreenUtils;

/**
 * Created by huangjinlong on 2016/5/13.
 */
public class GetClientIdModel implements IGetModel{
    @Override
    public void getNetWorkStringData(String url, Callback callback, String... parameter){

        Context context = MyApplication.getInstance();
        //获取手机设备唯一标示符
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tm.getDeviceId();
        if (TextUtils.isEmpty(deviceId)) {
            //如果没获取到设备ID  那么首先从SharePre里面查看之前是否保存过
            String id = (String) SPUtils.get(context, "device_id", "");
            if (TextUtils.isEmpty(id)) {
                //如果之前没保存过  那么生成一个并保存
                UUID uuid = UUID.randomUUID();
                id = uuid.toString();
                deviceId = id;
                SPUtils.put(context, "device_id", id);
            } else {
                //保存过  则把这个id 作为唯一标识符
                deviceId = id;
            }
        }
        // 获取手机产品信息
        String phoneInfo = "VERSION.RELEASE:" + android.os.Build.VERSION.RELEASE + ",";
        phoneInfo += "Product:" + android.os.Build.PRODUCT + ",";
        phoneInfo += "CPU_ABI:" + android.os.Build.CPU_ABI + ",";
        phoneInfo += "TAGS:" + android.os.Build.TAGS + ",";
        phoneInfo += "VERSION_CODES.BASE:"
                + android.os.Build.VERSION_CODES.BASE + ",";
        phoneInfo += "MODEL:" + android.os.Build.MODEL + ",";
        phoneInfo += "SDK:" + android.os.Build.VERSION.SDK + ",";
        phoneInfo += "DEVICE:" + android.os.Build.DEVICE + ",";
        phoneInfo += "DISPLAY:" + android.os.Build.DISPLAY + ",";
        phoneInfo += "BRAND:" + android.os.Build.BRAND + ",";
        phoneInfo += "BOARD:" + android.os.Build.BOARD + ",";
        phoneInfo += "FINGERPRINT:" + android.os.Build.FINGERPRINT + ",";
        phoneInfo += "ID:" + android.os.Build.ID + ",";
        phoneInfo += "MANUFACTURER:" + android.os.Build.MANUFACTURER + ",";
        phoneInfo += "USER:" + android.os.Build.USER;
        String versionCode = null;
        try {
            versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        phoneInfo = phoneInfo.replaceAll(" ", "");
        StringBuilder sb = new StringBuilder();
        sb.append(appendSuffix(url));
        sb.append("app_key=");
        sb.append(AppConfig.APP_KEY);
        sb.append("&device_id=");
        sb.append(deviceId);
        sb.append("&os=android");
        sb.append("&version=");
        sb.append(versionCode);
        sb.append("&resolution=");
        sb.append(ScreenUtils.getScreenHeight(context) * ScreenUtils.getScreenWidth(context));
        sb.append("&device_info=");
        sb.append(phoneInfo);
        sb.append("&channel=");
        sb.append("123456");
        OkHttpUtils.get()
                .url(sb.toString(), true)
                .build()
                .execute(callback);
    }

    static String appendSuffix(String url) {
        return url + getNextUrlSymbol(url);
    }
    static String getNextUrlSymbol(String url) {
        char last = url.charAt(url.length() - 1);
        String startChar = "";
        if (last != '?' && last != '&') {
            if (url.contains("?"))
                startChar = "&";
            else
                startChar = "?";
        }
        return startChar;
    }
}
